package com.changgou.controller;

import com.changgou.goods.pojo.Sku;
import com.changgou.search.feign.SkuInFeign;
import com.changgou.search.pojo.SkuInfo;
import entity.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;
import java.util.Set;

/**
 * @ClassName SearchController
 * @Description
 * @Author Mr.L
 * @Date 19:15 2019/11/18
 * @Version 2.1
 **/
@Controller
@RequestMapping("/search")
public class SearchController {

    @Autowired(required = false)
    private SkuInFeign skuInFeign;

    /**
     * 返回到搜索页面
     * @param searchMap
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@RequestParam(required = false) Map<String, String> searchMap, Model model){
        //替换特殊字符
        handlerSearchMap(searchMap);

        //调用搜索服务
        Map<String, Object> resultMap = skuInFeign.search(searchMap);

        //将检索条件和返回的结果放入model域用于页面回显
        model.addAttribute("searchMap", searchMap);
        model.addAttribute("resultMap", resultMap);

        //组装url
        String[] url = getUrl(searchMap);
        model.addAttribute("url",url[0]);
        model.addAttribute("sorturl",url[1]);

        //分页计算
        Page<SkuInfo> page = new Page<>(
                Long.parseLong(resultMap.get("totalElements").toString()),  //总页数
                Integer.parseInt(resultMap.get("pageNum").toString()),      //当前页码
                Integer.parseInt(resultMap.get("pageSize").toString())      //每页显示的条数
        );
        model.addAttribute("page",page);


        //返回搜索页面
        return "search";
    }


    /**
     * 组装url
     * @param searchMap
     * @return
     */
    private String[] getUrl(Map<String, String> searchMap){
        String url = "/search/list";
        String sorturl = "/search/list";
        if (searchMap != null && searchMap.size() > 0 ){
            url += "?";
            sorturl += "?";
            Set<Map.Entry<String, String>> entries = searchMap.entrySet();
            for (Map.Entry<String, String> entry : entries){
                String key = entry.getKey();
                String value = entry.getValue();
                if (key.equals("pageNum")){
                    continue;
                }
                url += key + "=" + value + "&";

                //只针对排序的链接地址
                if (key.equals("sortRule") || key.equals("sortField")){
                    continue;
                }
                sorturl += key + "=" + value + "&";
            }
            url = url.substring(0, url.length() - 1 );
            sorturl = sorturl.substring(0, sorturl.length() - 1 );
        }
        return new String[]{url, sorturl};
    }

    /**
     * 替换特殊字符
     * @param searchMap
     */
    public void handlerSearchMap(Map<String, String> searchMap){
        if (searchMap != null){
            for (Map.Entry<String, String> entry : searchMap.entrySet()){
                if (entry.getKey().startsWith("spec_")){
                    entry.setValue(entry.getValue().replace("+" , "%2B"));
                }
            }
        }
    }

}
